package com.vhall.component.controller.room;

import cn.hutool.core.collection.CollectionUtil;
import com.vhall.component.controller.room.facade.RoomAdminFacade;
import com.vhall.component.entity.inav.vo.InavRoomGetRespVO;
import com.vhall.component.entity.room.enums.LiveRoomTypeEnum;
import com.vhall.component.entity.room.vo.*;
import com.vhall.component.framework.common.annotation.VhallComponent;
import com.vhall.component.framework.common.constant.CommonConstant;
import com.vhall.component.framework.common.exception.BusinessException;
import com.vhall.component.framework.common.utils.StringUtils;
import com.vhall.component.plugin.common.annotation.TokenAuth;
import com.vhall.component.plugin.common.enums.OperateTypeEnum;
import com.vhall.component.plugin.common.exception.BizErrorCode;
import com.vhall.component.service.annotation.OperateLog;
import com.vhall.component.service.room.RoomAdminService;
import com.vhall.component.service.room.RoomConsoleService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author guoxin.wang
 * @date 2021/7/29 20:15
 */
@VhallComponent
public class RoomAdminController implements RoomAdminFacade {

    @Autowired
    private RoomAdminService roomAdminService;

    @Autowired
    private RoomConsoleService roomConsoleService;


    /**
     * 直播间列表
     *
     * @param reqVO 参数
     * @return 直播间分页列表
     */
    @Override
    @TokenAuth("sys:room:list")
    public RoomAdminListRspVO list(RoomAdminListReqVO reqVO) {
        reqVO.setDefault();
        setKeyWord(reqVO);
        return roomAdminService.list(reqVO);
    }

    private void setKeyWord(RoomAdminListReqVO reqVO) {
        if (StringUtils.isNotEmpty(reqVO.getKeyWord())) {
            String keyWord = reqVO.getKeyWord();
            if (reqVO.getKeyWord().contains(CommonConstant.UNDERLINE)) {
                keyWord = reqVO.getKeyWord().replace(CommonConstant.UNDERLINE, "\\" + CommonConstant.UNDERLINE);
            }
            if (reqVO.getKeyWord().contains(CommonConstant.PERCENT_SIGN)) {
                keyWord = keyWord.replace(CommonConstant.PERCENT_SIGN, "\\" + CommonConstant.PERCENT_SIGN);
            }
            reqVO.setKeyWord(keyWord);
        }
    }

    @Override
    public RoomAdminListRspVO examineListLive(RoomAdminListReqVO reqVO) {
        reqVO.setDefault();
        setKeyWord(reqVO);
        reqVO.setLiveTypes(CollectionUtil.newArrayList(LiveRoomTypeEnum.LIVE_BROADCAST.getType(), LiveRoomTypeEnum.FAKE_LIVE_BROADCAST.getType()));
        return roomAdminService.examineListLive(reqVO);
    }

    @Override
    public RoomAdminListRspVO examineListVideo(RoomAdminListReqVO reqVO) {
        reqVO.setDefault();
        setKeyWord(reqVO);
        reqVO.setLiveTypes(CollectionUtil.newArrayList(LiveRoomTypeEnum.ON_DEMAND.getType()));
        return roomAdminService.examineListLive(reqVO);
    }

    @Override
    @TokenAuth
    public InavRoomGetRespVO getRoomInfo(RoomAdminGetInfoVO reqVO) {
        if (reqVO.getIlId() != null) {
            return roomConsoleService.getRoomByIlId(reqVO);
        }

        if (org.apache.commons.lang3.StringUtils.isNotBlank(reqVO.getRoomId())) {
            return roomConsoleService.getRoom(reqVO.getRoomId(), null);
        }

        return new InavRoomGetRespVO();

    }

    @Override
    @TokenAuth("sys:room:del")
    @OperateLog(module = "房间管理-管理端", type = OperateTypeEnum.DELETE, name = "删除房间信息")
    public List<String> delete(RoomAdminDelVO reqVO) {
        return roomAdminService.delete(reqVO);
    }

    @Override
    @TokenAuth("sys:room:status")
    public Boolean searchStatus() {
        return true;
    }

    @Override
    @TokenAuth
    @OperateLog(module = "管理端-直播大厅", type = OperateTypeEnum.UPDATE, name = "设为热点")
    public Boolean setAsHotspot(RoomOperationStatusVO vo) {
        return roomAdminService.setAsHotspot(vo);
    }

    @Override
    @TokenAuth
    @OperateLog(module = "管理端-直播大厅", type = OperateTypeEnum.UPDATE, name = "推荐到优理宝首页")
    public Boolean recommendedHomePage(RoomOperationStatusVO vo) {
        return roomAdminService.recommendedHomePage(vo);
    }

    @Override
    @TokenAuth
    @OperateLog(module = "管理端-直播大厅", type = OperateTypeEnum.UPDATE, name = "上下架")
    public Boolean loadOrUnload(RoomOperationStatusVO vo) {
        return roomAdminService.loadOrUnload(vo);
    }

    @Override
    @TokenAuth
    @OperateLog(module = "管理端-直播大厅", type = OperateTypeEnum.UPDATE, name = "置顶")
    public Boolean topRoom(RoomOperationStatusVO vo) {
        return roomAdminService.topRoom(vo);
    }


    @Override
    @TokenAuth
    @OperateLog(module = "管理端-直播大厅", type = OperateTypeEnum.UPDATE, name = "编辑")
    public Boolean update(CreateRoomNewVO reqVO) {
        return roomConsoleService.update(reqVO);
    }

    @Override
    public Boolean saveWarm(WarmVO reqVO) {
        if (reqVO.getType() != null && reqVO.getType() != 1 && reqVO.getType() != 0) {
            throw new BusinessException(BizErrorCode.BIZ_TYPE_NOT_ALLOWED);
        }
        // 校验如果type传递的是空串的话，因为我们后端的type是Integer，所以后端会设置成null.所以在这里进行设置。
        if (null == reqVO.getType()) {
            reqVO.setType(0);
        }
        return roomConsoleService.saveWarm(reqVO, null);
    }

}
